package cn.wizzer.app.task.commons.ext.quartz.job;

import cn.wizzer.framework.util.ScriptUtil;
import org.nutz.ioc.impl.PropertiesProxy;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.log.Log;
import org.nutz.log.Logs;
import org.quartz.Job;
import org.quartz.JobDataMap;
import org.quartz.JobExecutionContext;

@IocBean
public class BackUpJob implements Job {

    @Inject
    private PropertiesProxy conf;

    private static final Log log = Logs.get();

    @Override
    public void execute(JobExecutionContext context) {
        // 备份数据
        try {
            JobDataMap jobDataMap = context.getJobDetail().getJobDataMap();
            String filePath = jobDataMap.getString("basePath") + "/databases/$(date \"+%Y-%m-%d&%H:%M:%T\")";
            String script =
                    "mkdir -p " + filePath
                            + "\n"
                            + "mysqldump -u" + conf.get("jdbc.username")
                            + " -p " + conf.get("jdbc.password")
                            + " --databases nutzwk_nb>"
                            + filePath +
                            "/backup.sql";
            ScriptUtil.execute(script);
        } catch (Exception e) {
            log.error(e.getMessage());
        }
    }

    public static void main(String[] args) {
        String filePath = "/data/nutzwk" + "/databases/$(date \"+%Y-%m-%d&%H:%M:%T\")";
        String script =
                "mkdir -p " + filePath
                        + "\n"
                        + "mysqldump -u" + "root"
                        + " -p " + "123456"
                        + " --databases nutzwk_nb>"
                        + filePath +
                        "/backup.sql";
        ScriptUtil.execute(script);
        System.out.println(script);
    }
}
